package com.sgam.mf.web;

import java.security.Principal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.common.wrapper.JQGridListWrapper;
import com.sgam.mf.domain.entity.ContractPayments;
import com.sgam.mf.domain.entity.Contracts;
import com.sgam.mf.domain.entity.ContractsList;
import com.sgam.mf.domain.entity.RegCompanies;
import com.sgam.mf.domain.entity.RegCompaniesShort;
import com.sgam.mf.domain.entity.RegPartners;
import com.sgam.mf.domain.entity.RegPersons;
import com.sgam.mf.domain.entity.RegPersonsShort;
import com.sgam.mf.domain.entity.VSalariati;
import com.sgam.mf.domain.entity.VreportContractPayments;
import com.sgam.mf.domain.ref.RefActivityType;
import com.sgam.mf.domain.ref.RefBank;
import com.sgam.mf.domain.ref.RefBranch;
import com.sgam.mf.domain.ref.RefCityHall;
import com.sgam.mf.domain.ref.RefCompany;
import com.sgam.mf.domain.ref.RefDomains;
import com.sgam.mf.domain.ref.RefPropertyType;
import com.sgam.mf.domain.ref.RefRegion;
import com.sgam.mf.domain.ref.RefRole;
import com.sgam.mf.domain.ref.RefServices;
import com.sgam.mf.domain.sec.SecSeq;
import com.sgam.mf.domain.sec.SecUser;
import com.sgam.mf.domain.sys.SysMainMenu;
import com.sgam.mf.domain.sys.TreeDomains;
import com.sgam.mf.service.entity.ContractPaymentsService;
import com.sgam.mf.service.entity.ContractsListService;
import com.sgam.mf.service.entity.ContractsService;
import com.sgam.mf.service.entity.RegCompaniesService;
import com.sgam.mf.service.entity.RegCompaniesShortService;
import com.sgam.mf.service.entity.RegPartnersService;
import com.sgam.mf.service.entity.RegPersonsService;
import com.sgam.mf.service.entity.RegPersonsShortService;
import com.sgam.mf.service.entity.VSalariatiService;
import com.sgam.mf.service.entity.VreportContractPaymentsService;
import com.sgam.mf.service.ref.RefActivityTypeService;
import com.sgam.mf.service.ref.RefBankService;
import com.sgam.mf.service.ref.RefBranchService;
import com.sgam.mf.service.ref.RefCityHallService;
import com.sgam.mf.service.ref.RefCompanyService;
import com.sgam.mf.service.ref.RefDomainsService;
import com.sgam.mf.service.ref.RefPropertyTypeService;
import com.sgam.mf.service.ref.RefRegionService;
import com.sgam.mf.service.ref.RefRoleService;
import com.sgam.mf.service.ref.RefServicesService;
import com.sgam.mf.service.sec.SecSeqService;
import com.sgam.mf.service.sec.SecUserService;
import com.sgam.mf.service.sys.SysMainMenuService;
import com.sgam.mf.service.sys.TreeDomainsService;

@Controller
@RequestMapping("/json")
public class JSONController {
		
	protected final Logger log = Logger.getLogger(getClass());
	
/* ******************************   REFERENCIES              ****************************** */	
	@Autowired private RefRoleService 					refRoleService;
	@Autowired private RefCompanyService 				refCompanyService;
	@Autowired private RefBankService 					refBankService;
	@Autowired private RefActivityTypeService			refActivityTypeService;	
	@Autowired private RefDomainsService				refDomainsService;
	@Autowired private RefServicesService				refServicesService;
	@Autowired private RefBranchService					refBranchService;
	@Autowired private RefPropertyTypeService			refPropertyTypeService;
	@Autowired private RefRegionService					refRegionService;
	@Autowired private RefCityHallService				refCityHallService;
	
	
/* ******************************   REGISTRY	              ****************************** */	
	@Autowired private RegPersonsService 				regPersonsService;
	@Autowired private RegPersonsShortService 			regPersonsShortService;
	@Autowired private RegCompaniesService 				regCompaniesService;
	@Autowired private RegCompaniesShortService 		regCompaniesShortService;
	@Autowired private RegPartnersService 				regPartnersService;

/* ******************************   ENTITY		              ****************************** */	
	
	@Autowired private ContractsService 				contractsService;
	@Autowired private ContractsListService 			contractsListService;
	@Autowired private VreportContractPaymentsService	vreportContractPaymentsService;
	@Autowired private ContractPaymentsService			contractPaymentsService;
	@Autowired private VSalariatiService				vSalariatiService;
		
	
/* ******************************   SECURITY	              ****************************** */	
	@Autowired private SecUserService					secUserService;
	@Autowired private SecSeqService					secSeqService;
	
/* ******************************   SYSTEM		              ****************************** */
	@Autowired private SysMainMenuService				sysMainMenuService;
	@Autowired private TreeDomainsService				treeDomainsService;
	
/* ************************************************************************************************************ 
 * 
 */	
    @RequestMapping("/ping")
    public @ResponseBody String ping(WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - ping ");
    	
    	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  	   	log.debug("Authentication auth  = " + auth.getName());
  	   	
    	String result = "Hello from JSON Controller";
        return  result;
    }  
    

    
    
/* ************************************************************************************************************ 
 * 						REFERENCIES
 ************************************************************************************************************** */    
    @RequestMapping("/listRefRole")
    public @ResponseBody List<RefRole>  listRefRole (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefRole ");
    	List<RefRole>   result = refRoleService.list(secUserService.getProxyCurrentUser());
        return  result;
    }
    
    /* *********************************************************************************************************** */
    @RequestMapping("/listRefCompany")
    public @ResponseBody List<RefCompany>  listRefCompany (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefCompany ");
    	List<RefCompany>   result = refCompanyService.list(secUserService.getProxyCurrentUser());
        return  result;
    }    

    /* *********************************************************************************************************** */
    @RequestMapping("/listRefBank")
    public @ResponseBody JQGridListWrapper<RefBank>  listRefBank (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefBank ");

    	List<RefBank>   result = refBankService.list(secUserService.getProxyCurrentUser());
        return  new JQGridListWrapper<RefBank> (result.size(), 1, 10, result);
    }    
        
    /* *********************************************************************************************************** */
    @RequestMapping("/listRefActivityType")
    public @ResponseBody List<RefActivityType>  listRefActivityType (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefActivityType ");
    	List<RefActivityType>   result = refActivityTypeService.list(secUserService.getProxyCurrentUser());
        return  result;
    }  
    
    /* *********************************************************************************************************** */
    @RequestMapping("/listRefDomains")
    public @ResponseBody JQGridListWrapper<RefDomains>  listRefDomains (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefDomains ");

    	List<RefDomains>   result = refDomainsService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  new JQGridListWrapper<RefDomains> (result.size(), 1, 10, result);
    }   

    /* *********************************************************************************************************** */
    @RequestMapping("/listRefDomainsCBX")
    public @ResponseBody List<RefDomains>  listRefDomainsCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefDomainsCBX ");
    	List<RefDomains>   result = refDomainsService.list(secUserService.getProxyCurrentUser());
        return  result;
    }      
    
    /* *********************************************************************************************************** */
    @RequestMapping("/listRefServicesCBX")
    public @ResponseBody List<RefServices>  listRefServicesCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefServicesCBX ");
    	List<RefServices>   result = refServicesService.list(secUserService.getProxyCurrentUser());
        return  result;
    }  
    /* *********************************************************************************************************** */
    
    @RequestMapping(value="/listRefServicesByDomainCBX", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefServices>  listRefServicesByDomainCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefServicesByDomainCBX ");
    	List<RefServices> result = refServicesService.listByDomain(Integer.valueOf(request.getParameter("id")));
        return  result;
    }  
    
    @RequestMapping(value="/listRefRegionsCBX", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefRegion>  listRefRegionsCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefRegionsCBX ");
    	List<RefRegion> result = refRegionService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
        return  result;
    }      
    
    @RequestMapping(value="/listRefCityHallsCBX", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefCityHall>  listRefCityHallsCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefCityHallsCBX ");
    	List<RefCityHall> result = refCityHallService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
        return  result;
    }  
    
    @RequestMapping(value="/listRefCityHallsByRegionCBX", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefCityHall>  listRefCityHallsByRegionCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefCityHallsByRegionCBX ");
    	List<RefCityHall> result = refCityHallService.listByRegion(Integer.valueOf(request.getParameter("id")));
    	Collections.sort(result);
        return  result;
    }  
    
    /* *********************************************************************************************************** */
    /*      DOMENII
    /* *********************************************************************************************************** */
    @RequestMapping(value="/listRefDomainsREST/List" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefDomains>  listRefDomainsRESTList () {

    	log.debug("JSONController - listRefDomainsRESTList ");
    	

    	List<RefDomains>   result = refDomainsService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefDomainsREST/Create", method = {RequestMethod.POST})
    public @ResponseBody RefDomains  listRefDomainsRESTCreate  (@RequestBody RefDomains  item) {
    	
    	log.debug("JSONController - listRefDomainsRESTCreate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refDomainsService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefDomainsREST/Update", method = { RequestMethod.POST})
    public @ResponseBody RefDomains  listRefDomainsRESTUpdate  (@RequestBody RefDomains  item) {

    	log.debug("JSONController - listRefDomainsRESTUpdate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refDomainsService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefDomainsREST/Destroy", method = { RequestMethod.POST})
    public @ResponseBody RefDomains  listRefDomainsRESTDestroy  (@RequestBody RefDomains  item) {

    	log.debug("JSONController - listRefDomainsRESTDestroy");
    	item.setStatus(0);
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());
    	 
    	return  refDomainsService.update(item, secUserService.getProxyCurrentUser());
    }     
    
    /* *********************************************************************************************************** */
    /*      SERVICII
    /* *********************************************************************************************************** */    
    @RequestMapping(value="/listRefServicesREST/List" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefServices>  listRefServicesRESTList () {

    	log.debug("JSONController - listRefServicesRESTList ");

    	List<RefServices>   result = refServicesService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefServicesREST/Create", method = {RequestMethod.POST})
    public @ResponseBody RefServices  listRefServicesRESTCreate  (@RequestBody RefServices  item) {
    	
    	log.debug("JSONController - listRefServicesRESTCreate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refServicesService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefServicesREST/Update", method = { RequestMethod.POST})
    public @ResponseBody RefServices  listRefServicesRESTUpdate  (@RequestBody RefServices  item) {

    	log.debug("JSONController - listRefServicesRESTUpdate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refServicesService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefServicesREST/Destroy", method = { RequestMethod.POST})
    public @ResponseBody RefServices  listRefServicesRESTDestroy  (@RequestBody RefServices  item) {

    	log.debug("JSONController - listRefServicesRESTDestroy");
    	item.setStatus(0);
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());
    	 
    	return  refServicesService.update(item, secUserService.getProxyCurrentUser());
    }      
    /* *********************************************************************************************************** */
    /*      FILIALE
    /* *********************************************************************************************************** */  
    @RequestMapping(value="/listRefBranchREP" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefBranch>  listRefBranchREP () {

    	log.debug("JSONController - listRefBranchREP ");

    	List<RefBranch>   result = refBranchService.list(secUserService.getProxyCurrentUser());
    	RefBranch element = new RefBranch();
    	element.setId(-1);
    	element.setMdValue("-Toate-");
    	result.add(element);
    	//result.add(new RefBranch())
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefBranchCBX" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefBranch>  listRefBranchCBXt () {

    	log.debug("JSONController - listRefBranchCBXt ");

    	List<RefBranch>   result = refBranchService.list(secUserService.getProxyCurrentUser());
    	//result.add(0, element)
    	Collections.sort(result);
    	return  result;
    }
    
    
    @RequestMapping(value="/listRefBranchREST/List" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefBranch>  listRefBranchRESTList () {

    	log.debug("JSONController - listRefBranchRESTList ");

    	List<RefBranch>   result = refBranchService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefBranchREST/Create", method = {RequestMethod.POST})
    public @ResponseBody RefBranch  listRefBranchRESTCreate  (@RequestBody RefBranch  item) {
    								
    	log.debug("JSONController - listRefBranchRESTCreate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refBranchService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefBranchREST/Update", method = { RequestMethod.POST})
    public @ResponseBody RefBranch  listRefBranchRESTUpdate  (@RequestBody RefBranch  item) {

    	log.debug("JSONController - listRefBranchRESTUpdate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refBranchService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefBranchREST/Destroy", method = { RequestMethod.POST})
    public @ResponseBody RefBranch  listRefBranchRESTDestroy  (@RequestBody RefBranch  item) {

    	log.debug("JSONController - listRefBranchRESTDestroy");
    	item.setStatus(0);
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());
    	 
    	return  refBranchService.update(item, secUserService.getProxyCurrentUser());
    } 

    /* *********************************************************************************************************** */
    /*      TIPURI ACTIVITATI
    /* *********************************************************************************************************** */   
    @RequestMapping(value="/listRefActivityTypeREST/List" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefActivityType>  listRefActivityTypeRESTList () {

    	log.debug("JSONController - listRefActivityTypeRESTList ");

    	List<RefActivityType>   result = refActivityTypeService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefActivityTypeREST/Create", method = {RequestMethod.POST})
    public @ResponseBody RefActivityType listRefActivityTypeRESTCreate  (@RequestBody RefActivityType  item) {
    	
    	log.debug("JSONController - listRefActivityTypeRESTCreate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refActivityTypeService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefActivityTypeREST/Update", method = { RequestMethod.POST})
    public @ResponseBody RefActivityType  listRefActivityTypeRESTUpdate  (@RequestBody RefActivityType  item) {

    	log.debug("JSONController - listRefActivityTypeRESTUpdate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refActivityTypeService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefActivityTypeREST/Destroy", method = { RequestMethod.POST})
    public @ResponseBody RefActivityType  listRefActivityTypeRESTDestroy  (@RequestBody RefActivityType  item) {

    	log.debug("JSONController - listRefActivityTypeRESTDestroy");
    	item.setStatus(0);
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());
    	 
    	return  refActivityTypeService.update(item, secUserService.getProxyCurrentUser());
    }     
    
    
    /* *********************************************************************************************************** */
    /*      TIPURI PROPRITATII
    /* *********************************************************************************************************** */   
    @RequestMapping(value="/listRefPropertyTypeREST/List" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefPropertyType>  listRefPropertyTypeRESTList () {

    	log.debug("JSONController - listRefPropertyTypeRESTList ");

    	List<RefPropertyType>   result = refPropertyTypeService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefPropertyTypeREST/Create", method = {RequestMethod.POST})
    public @ResponseBody RefPropertyType listRefPropertyTypeRESTCreate  (@RequestBody RefPropertyType  item) {
    	
    	log.debug("JSONController - listRefPropertyTypeRESTCreate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refPropertyTypeService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefPropertyTypeREST/Update", method = { RequestMethod.POST})
    public @ResponseBody RefPropertyType  listRefPropertyTypeRESTUpdate  (@RequestBody RefPropertyType  item) {

    	log.debug("JSONController - listRefPropertyTypeRESTUpdate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refPropertyTypeService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefPropertyTypeREST/Destroy", method = { RequestMethod.POST})
    public @ResponseBody RefPropertyType  listRefPropertyTypeRESTDestroy  (@RequestBody RefPropertyType  item) {

    	log.debug("JSONController - listRefPropertyTypeRESTDestroy");
    	item.setStatus(0);
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());
    	 
    	return  refPropertyTypeService.update(item, secUserService.getProxyCurrentUser());
    }     
    
    
    /* *********************************************************************************************************** */
    /*      BANCILE
    /* *********************************************************************************************************** */   
    @RequestMapping(value="/listRefBankREST/List" , method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<RefBank>  listRefBankRESTList () {

    	log.debug("JSONController - listRefBankRESTList ");

    	List<RefBank>   result = refBankService.list(secUserService.getProxyCurrentUser());
    	Collections.sort(result);
    	return  result;
    }
    
    @RequestMapping(value="/listRefBankREST/Create", method = {RequestMethod.POST})
    public @ResponseBody RefBank listRefBankRESTCreate  (@RequestBody RefBank  item) {
    	
    	log.debug("JSONController - listRefBankRESTCreate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refBankService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefBankREST/Update", method = { RequestMethod.POST})
    public @ResponseBody RefBank  listRefBankRESTUpdate  (@RequestBody RefBank  item) {

    	log.debug("JSONController - listRefBankRESTUpdate");
    	
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());

    	return  refBankService.update(item, secUserService.getProxyCurrentUser());
    }    
    
    @RequestMapping(value="/listRefBankREST/Destroy", method = { RequestMethod.POST})
    public @ResponseBody RefBank  listRefBankRESTDestroy  (@RequestBody RefBank  item) {

    	log.debug("JSONController - listRefBankRESTDestroy");
    	item.setStatus(0);
    	item.setUpdatedBy(secUserService.getCurrentUserId());
    	item.setUpdatedDate(new Date());
    	 
    	return  refBankService.update(item, secUserService.getProxyCurrentUser());
    }        
    
        
/* ************************************************************************************************************ 
 * 						REGISTRYS
 ************************************************************************************************************** */
    @RequestMapping("/listRegPersons")
    public @ResponseBody List<RegPersonsShort>  listRegPersons (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRegPersons ");

    	List<RegPersonsShort>   result = regPersonsShortService.list(secUserService.getProxyCurrentUser());
        return  result;
    }    
    
        
    @RequestMapping("/regPersons")
    public @ResponseBody RegPersonsShort  regPersons (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - regPersons ");
    	log.debug("JSONController - regPersons Id=" + (request.getParameter("id")));
    	
    	//RegPersons  result = regPersonsService.get(Integer.valueOf(request.getParameter("id")), secUserService.getProxyCurrentUser());
    	RegPersonsShort  result = regPersonsShortService.get(Integer.valueOf(request.getParameter("id")), secUserService.getProxyCurrentUser());

        return  result;
    }   
 
    /* *********************************************************************************************************** */  
    @RequestMapping("/listRegCompanies")
    public @ResponseBody List<RegCompaniesShort>  listRegCompanies (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRegCompanies ");

    	List<RegCompaniesShort>   result = regCompaniesShortService.list(secUserService.getProxyCurrentUser());
        //return new  JQGridListWrapper<RegCompanies> (result.size() , 1 , 10, result );
    	return result;
    }  
    
    @RequestMapping("/regCompanies")
    public @ResponseBody RegCompaniesShort  regCompanies (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - regCompanies ");

    	//RegCompanies  result = regCompaniesService.get(Integer.valueOf(request.getParameter("id")), secUserService.getProxyCurrentUser());
    	RegCompaniesShort  result = regCompaniesShortService.get(Integer.valueOf(request.getParameter("id")), secUserService.getProxyCurrentUser());

        return  result;
    }   
    
    
    /* *********************************************************************************************************** */  
    @RequestMapping("/listRegPartners")
    public @ResponseBody List<RegPartners>  listRegPartners (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRegCompanies ");

    	List<RegPartners>   result = regPartnersService.list(secUserService.getProxyCurrentUser());
        //return new  JQGridListWrapper<RegCompanies> (result.size() , 1 , 10, result );
    	return result;
    }  
    
    @RequestMapping("/regPartners")
    public @ResponseBody RegPartners  regPartners (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - regCompanies ");

    	RegPartners  result = regPartnersService.get(Integer.valueOf(request.getParameter("id")), secUserService.getProxyCurrentUser());

        return  result;
    }   
    
/* ************************************************************************************************************ 
 * 						ENTITYS
 ************************************************************************************************************** */
//    @RequestMapping("/listContracts1")
//    public @ResponseBody List<ContractsList>  listContracts1 (WebRequest request, Principal principal, Locale locale) {
//    	log.debug("JSONController - listContracts ");
//
//    	List<ContractsList>   result = contractsListService.list(secUserService.getProxyCurrentUser(), 
//    													 Integer.valueOf(request.getParameter("listType")));
////    	Collections.sort(result);
//        return  result; //new  JQGridListWrapper<Contracts> (result.size() , 1 , 10, result );
//    }    
    
    /* ************************************************************************************************************ 
     * 						ENTITYS
     ************************************************************************************************************** */
        @RequestMapping("/listContracts")
        public @ResponseBody List<ContractsList>  listContracts (WebRequest request, Principal principal, Locale locale) {
        	log.debug("JSONController - listContracts ");

        	List<ContractsList>   result = contractsListService.shortList(secUserService.getProxyCurrentUser(), 
        													 Integer.valueOf(request.getParameter("listType")));
//        	Collections.sort(result);
            return  result; //new  JQGridListWrapper<Contracts> (result.size() , 1 , 10, result );
        }  
    
        @RequestMapping("/listContractForPayments")
        //        public @ResponseBody JQGridListWrapper<VreportContractPayments>  listContractForPayments (WebRequest request, Principal principal, Locale locale) {
        public @ResponseBody List<VreportContractPayments>  listContractForPayments (WebRequest request, Principal principal, Locale locale) {
                	log.debug("JSONController - listContractForPayments ");
        	Integer vUid=null;
        	
        	if (request.getParameter("pUid")!=null) {
        		vUid=Integer.valueOf(request.getParameter("pUid"));
        	};
        		
        	List<VreportContractPayments>   result = vreportContractPaymentsService.list(secUserService.getProxyCurrentUser(),
        																							vUid);
 //       	Collections.sort(result);
        	//           return  new  JQGridListWrapper<VreportContractPayments> (result.size() , 1 , 10, result );
        	return result;
        }  
        
        @RequestMapping("/listContractPayments")
        public @ResponseBody JQGridListWrapper<ContractPayments>  listContractPayments (WebRequest request, Principal principal, Locale locale) {
        	log.debug("JSONController - listContractPayments ");
        	Integer vPersonalId=null;
        	Integer vPartnerId=null;
        	Integer vContractId=null;
        	
        	//if (request.getParameter("pContractId")!=null) {
        		vContractId=Integer.valueOf(request.getParameter("pContractId"));
        	//};
        	
        	if (request.getParameter("pPersonalId")!=null) {
        		if (request.getParameter("pPersonalId").toString().length()>0){
        		vPersonalId=Integer.valueOf(request.getParameter("pPersonalId"));
        		}
        	};
        	
        	if (request.getParameter("pPartnerId")!=null) {
        		if (request.getParameter("pPartnerId").toString().length()>0) {
        			vPartnerId=Integer.valueOf(request.getParameter("pPartnerId"));
        		}
        	}
        		
        	List<ContractPayments>   result = contractsService.listPay(secUserService.getProxyCurrentUser(),
        																							vContractId,
        																							vPersonalId,
        																							vPartnerId);
 //       	Collections.sort(result);
            return  new  JQGridListWrapper<ContractPayments> (result.size() , 1 , 10, result );
        }
//    @RequestMapping("/listContracts")
//    public @ResponseBody List<Contracts>  listContracts (WebRequest request, Principal principal, Locale locale) {
//    	log.debug("JSONController - listContracts ");
//
//    	List<Contracts>   result = contractsService.list(secUserService.getProxyCurrentUser(), 
//    													 Integer.valueOf(request.getParameter("listType")));
//    	Collections.sort(result);
//        return  result; //new  JQGridListWrapper<Contracts> (result.size() , 1 , 10, result );
//    }  
    
     @RequestMapping("/updContractPayments")
     public @ResponseBody Integer  updContractPayments (WebRequest request, Principal principal, Locale locale) {
     	log.debug("JSONController - updContractPayments ");
     	log.debug("JSONController - pAmount ="+request.getParameter("pAmount"));
     	
     	ContractPayments contractPayments = new ContractPayments ();
     	
     	contractPayments.setAmount(Long.valueOf(request.getParameter("pAmount")));
     	contractPayments.setContractIdFk(Integer.valueOf(request.getParameter("pContractId")));
     	contractPayments.setComments(request.getParameter("pComments"));
     	if (request.getParameter("pPersonalId")!=null) {
     		if (request.getParameter("pPersonalId").toString().length()>0){
     			contractPayments.setPersonalFk(Integer.valueOf(request.getParameter("pPersonalId")));
     		}
    	};
    	
    	if (request.getParameter("pPartnerId")!=null) {
    		if (request.getParameter("pPartnerId").toString().length()>0) {
    			contractPayments.setPartenerFk(Integer.valueOf(request.getParameter("pPartnerId")));
    		}
    	}
     	contractPayments.setDateTrans(new Date());

     	ContractPayments result=contractPaymentsService.update(contractPayments, secUserService.getProxyCurrentUser());

         return result.getId();
     }  
     
     @RequestMapping("/delContractPayments")
     public @ResponseBody Integer  delContractPayments (WebRequest request, Principal principal, Locale locale) {
     	log.debug("JSONController - delContractPayments ");
     	
     	contractPaymentsService.deleteById(Integer.valueOf(request.getParameter("pId")), secUserService.getProxyCurrentUser());

         return 1;
     }  
        
    @RequestMapping(value="/contracts" , method = {RequestMethod.GET, RequestMethod.POST}) 
    public @ResponseBody Contracts  contracts (WebRequest request, @RequestParam(required=false) Integer contractId) {
    	log.debug("JSONController - contracts ");
    	Contracts  result;
    	
    	if (contractId == null) {
    		result = contractsService.get(8361, 		secUserService.getProxyCurrentUser());
    	} else {
    		result = contractsService.get(contractId, 	secUserService.getProxyCurrentUser());
    	}
    	

        return  result;
    }   
    
/* ************************************************************************************************************ 
 * 						SECURITY
 ************************************************************************************************************** */     
    @RequestMapping("/listSecUsers")
    public @ResponseBody List<SecUser>  listSecUsers (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listSecUsers ");

    	List<SecUser>   result = secUserService.list(secUserService.getProxyCurrentUser());
        return result; 
    } 
    
    @RequestMapping("/testNewSecUsers")
    public @ResponseBody SecUser  testNewSecUsers (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - testNewSecUsers ");
    	
    	SecUser   result = secUserService.getNewUser();
        return result; 
    } 
    
    @RequestMapping("/seqNo")
    public @ResponseBody String  seqNo (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - seqNo ");
    	
//    	Integer vYear=new Date();
    	SecSeq seqSeq = secSeqService.get(1, secUserService.getProxyCurrentUser());
    	String   result =seqSeq.getSeqNo().toString();
    	
    	log.debug("JSONController - seqNo ="+result);
    	seqSeq.setSeqNo(seqSeq.getSeqNo()+1);
    	secSeqService.update(seqSeq, secUserService.getProxyCurrentUser());
    	result = String.format("%5s", result).replace(' ', '0')+"/14";
        return result;
    } 
    
/* ************************************************************************************************************ 
 * 						SYSTEM
 ************************************************************************************************************** */   
    @RequestMapping("/loadMainMenu")
    public @ResponseBody List<SysMainMenu>  loadMainMenu (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - loadMainMenu ");
    	List<SysMainMenu>		result = sysMainMenuService.listTree();
    	
 //   	log.debug("###############################################");
    	
 //   	printSysMenuItems(result);
    	
        return  result;
    }    
    
/* ************************************************************************************************************ 
 * 						Domains @ Services 
 ************************************************************************************************************** */   
        @RequestMapping("/domainsServicesTree")
        public @ResponseBody List<TreeDomains>  domainsServicesTree (WebRequest request, Principal principal, Locale locale) {
        	log.debug("JSONController - domainsServicesTree ");
        	List<TreeDomains>		result = treeDomainsService.list(secUserService.getProxyCurrentUser());
        	
        	Collections.sort(result);
        	
            return  result;
        }   
        
        @RequestMapping("/listSalariati")
        public @ResponseBody List<VSalariati>  listSalariati (WebRequest request, Principal principal, Locale locale) {
        	log.debug("JSONController - listSecUsers ");

        	List<VSalariati>   result = vSalariatiService.list(secUserService.getProxyCurrentUser());
            return result; 
        } 
    
    
}

